持续学习路径:Python编程课程

1. Python 简介和特性

1.1 Python 简介

Python 是一种高级编程语言,以其简洁易读的语法和强大的功能而闻名。它广泛应用于数据分析、Web开发、人工智能等领域。

1.2 Python 特性

  • 易读性:Python 语法接近自然语言,易于理解和编写。
  • 跨平台:Python 可以在 Windows、macOS 和 Linux 上运行。
  • 丰富的库:Python 拥有大量的标准库和第三方库,支持各种应用开发。

2. 环境搭建

2.1 安装 Python

  1. 访问 Python 官方网站 下载适合你操作系统的 Python 版本。
  2. 安装过程中,确保勾选“Add Python to PATH”选项。

2.2 IDE 选择

推荐使用以下 IDE:

  • PyCharm:功能强大的 IDE,适合大型项目。
  • VS Code:轻量级且高度可定制的编辑器。
  • Jupyter Notebook:适合数据分析和交互式编程。

3. 第一个 Python 程序

print("Hello, World!")

3.1 代码解释

  • print 是 Python 内置函数,用于输出内容到控制台。
  • "Hello, World!" 是要输出的字符串。

4. 变量和数据类型

4.1 变量

变量是存储数据的容器。Python 是动态类型语言,变量类型在运行时确定。

name = "Alice"
age = 25

4.2 数据类型

  • 整数 (int):age = 25
  • 浮点数 (float):height = 1.75
  • 字符串 (str):name = "Alice"
  • 布尔值 (bool):is_student = True

5. 基本运算符

5.1 算术运算符

  • +:加法
  • -:减法
  • *:乘法
  • /:除法
  • %:取余
a = 10
b = 3
print(a + b)  # 输出: 13
print(a % b)  # 输出: 1

5.2 比较运算符

  • ==:等于
  • !=:不等于
  • >:大于
  • <:小于
print(a > b)  # 输出: True

6. 条件语句

6.1 if 语句

if age >= 18:
    print("You are an adult.")

6.2 elif 语句

if age < 18:
    print("You are a minor.")
elif age == 18:
    print("You just turned 18!")

6.3 else 语句

if age < 18:
    print("You are a minor.")
else:
    print("You are an adult.")

7. 循环

7.1 for 循环

for i in range(5):
    print(i)

7.2 while 循环

count = 0
while count < 5:
    print(count)
    count += 1

8. 异常处理

8.1 try-except 语句

try:
    result = 10 / 0
except ZeroDivisionError:
    print("Cannot divide by zero.")

9. 列表和元组

9.1 列表

列表是可变的有序集合。

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)  # 输出: ['apple', 'banana', 'cherry', 'orange']

9.2 元组

元组是不可变的有序集合。

coordinates = (10, 20)
print(coordinates[0])  # 输出: 10

10. 字典和集合

10.1 字典

字典是键值对的集合。

person = {"name": "Alice", "age": 25}
print(person["name"])  # 输出: Alice

10.2 集合

集合是无序且不重复的元素集合。

unique_numbers = {1, 2, 3, 3, 4}
print(unique_numbers)  # 输出: {1, 2, 3, 4}

11. 字符串操作

11.1 字符串拼接

first_name = "Alice"
last_name = "Smith"
full_name = first_name + " " + last_name
print(full_name)  # 输出: Alice Smith

11.2 字符串格式化

age = 25
message = f"My name is {full_name} and I am {age} years old."
print(message)  # 输出: My name is Alice Smith and I am 25 years old.

12. 函数定义和调用

12.1 函数定义

def greet(name):
    return f"Hello, {name}!"

12.2 函数调用

print(greet("Alice"))  # 输出: Hello, Alice!

13. 参数和返回值

13.1 参数

def add(a, b):
    return a + b

13.2 返回值

result = add(3, 5)
print(result)  # 输出: 8

14. 模块导入和使用

14.1 导入模块

import math

14.2 使用模块

print(math.sqrt(16))  # 输出: 4.0

15. 包管理 (pip)

15.1 安装包

pip install requests

15.2 使用包

import requests
response = requests.get("https://api.github.com")
print(response.status_code)  # 输出: 200

16. 类和对象

16.1 定义类

class Dog:
    def __init__(self, name):
        self.name = name

    def bark(self):
        return f"{self.name} says woof!"

16.2 创建对象

my_dog = Dog("Buddy")
print(my_dog.bark())  # 输出: Buddy says woof!

17. 继承和多态

17.1 继承

class Animal:
    def speak(self):
        return "Some sound"

class Dog(Animal):
    def speak(self):
        return "Woof!"

17.2 多态

def animal_sound(animal):
    print(animal.speak())

animal_sound(Dog())  # 输出: Woof!

18. 封装和抽象

18.1 封装

class BankAccount:
    def __init__(self, balance):
        self.__balance = balance

    def deposit(self, amount):
        self.__balance += amount

    def get_balance(self):
        return self.__balance

18.2 抽象

from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius ** 2

19. 读写文本文件

19.1 写文件

with open("example.txt", "w") as file:
    file.write("Hello, World!")

19.2 读文件

with open("example.txt", "r") as file:
    content = file.read()
    print(content)  # 输出: Hello, World!

20. 处理 CSV, JSON 数据

20.1 处理 CSV

import csv

with open("data.csv", "r") as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

20.2 处理 JSON

import json

data = {"name": "Alice", "age": 25}
with open("data.json", "w") as file:
    json.dump(data, file)

with open("data.json", "r") as file:
    loaded_data = json.load(file)
    print(loaded_data)  # 输出: {'name': 'Alice', 'age': 25}

21. 文件系统操作

21.1 创建目录

import os

os.mkdir("new_directory")

21.2 列出文件

for file in os.listdir("."):
    print(file)

22. 生成器和迭代器

22.1 生成器

def count_up_to(n):
    i = 0
    while i < n:
        yield i
        i += 1

for number in count_up_to(5):
    print(number)  # 输出: 0 1 2 3 4

22.2 迭代器

class MyIterator:
    def __init__(self, limit):
        self.limit = limit
        self.current = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.current < self.limit:
            self.current += 1
            return self.current - 1
        else:
            raise StopIteration

for i in MyIterator(5):
    print(i)  # 输出: 0 1 2 3 4

23. 装饰器

23.1 定义装饰器

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

@my_decorator
def say_hello():
    print("Hello!")

say_hello()

24. 上下文管理器

24.1 使用 with 语句

class FileManager:
    def __init__(self, filename, mode):
        self.filename = filename
        self.mode = mode
        self.file = None

    def __enter__(self):
        self.file = open(self.filename, self.mode)
        return self.file

    def __exit__(self, exc_type, exc_value, traceback):
        self.file.close()

with FileManager("example.txt", "w") as file:
    file.write("Hello, World!")

25. 多线程和并发

25.1 多线程

import threading

def print_numbers():
    for i in range(5):
        print(i)

thread = threading.Thread(target=print_numbers)
thread.start()
thread.join()

26. datetime 和 time 模块

26.1 datetime 模块

from datetime import datetime

now = datetime.now()
print(now)  # 输出: 当前日期和时间

26.2 time 模块

import time

time.sleep(2)  # 暂停 2 秒

27. os 和 sys 模块

27.1 os 模块

import os

print(os.getcwd())  # 输出: 当前工作目录

27.2 sys 模块

import sys

print(sys.version)  # 输出: Python 版本

28. re 模块 (正则表达式)

28.1 正则表达式

import re

pattern = r"\d+"
text = "There are 123 apples."
match = re.search(pattern, text)
print(match.group())  # 输出: 123

29. collections 模块

29.1 Counter

from collections import Counter

fruits = ["apple", "banana", "apple", "cherry", "banana"]
fruit_count = Counter(fruits)
print(fruit_count)  # 输出: Counter({'apple': 2, 'banana': 2, 'cherry': 1})

30. NumPy 和 Pandas (数据分析)

30.1 NumPy

import numpy as np

array = np.array([1, 2, 3, 4, 5])
print(array)  # 输出: [1 2 3 4 5]

30.2 Pandas

import pandas as pd

data = {'name': ['Alice', 'Bob'], 'age': [25, 30]}
df = pd.DataFrame(data)
print(df)

31. Matplotlib (数据可视化)

31.1 绘制折线图

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.show()

32. Requests (HTTP 请求)

32.1 发送 GET 请求

import requests

response = requests.get("https://api.github.com")
print(response.status_code)  # 输出: 200

33. Flask/Django (Web 开发)

33.1 Flask 入门

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, World!"

if __name__ == "__main__":
    app.run(debug=True)

33.2 Django 入门

django-admin startproject myproject
cd myproject
python manage.py runserver

34. SQLite 基础

34.1 创建数据库

import sqlite3

conn = sqlite3.connect('example.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)''')
conn.commit()
conn.close()

35. MySQL/PostgreSQL 连接

35.1 MySQL 连接

import mysql.connector

conn = mysql.connector.connect(
    host="localhost",
    user="root",
    password="password",
    database="mydatabase"
)
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")
for row in cursor:
    print(row)
conn.close()

36. ORM 使用 (SQLAlchemy)

36.1 定义模型

from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)

engine = create_engine('sqlite:///example.db')
Base.metadata.create_all(engine)
Session = sessionmaker